load("//bazel:mongo_src_rules.bzl", "idl_generator", "mongo_cc_integration_test", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
        "*.cpp",
    ]),
)

mongo_cc_library(
    name = "remote_command",
    srcs = [
        "remote_command_request.cpp",
        "remote_command_response.cpp",
    ],
    deps = [
        "//src/mongo/db:api_parameters",
        "//src/mongo/db/auth:security_token_auth",
        "//src/mongo/db/sharding_environment:mongos_server_parameters",
        "//src/mongo/otel/traces:telemetry_context_serialization",
        "//src/mongo/otel/traces:tracing_enablement",
        "//src/mongo/rpc:metadata",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "task_executor_interface",
    srcs = [
        "task_executor.cpp",
    ],
    deps = [
        ":remote_command",
        "//src/mongo:base",
    ],
)

mongo_cc_library(
    name = "async_multicaster",
    srcs = [
        "async_multicaster.cpp",
    ],
    deps = [
        ":remote_command",
        ":task_executor_interface",
        "//src/mongo:base",
        "//src/mongo/client:retry_strategy",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "scoped_task_executor",
    srcs = [
        "scoped_task_executor.cpp",
    ],
    deps = [
        ":task_executor_interface",
    ],
)

mongo_cc_library(
    name = "network_interface",
    srcs = [
        "network_interface.cpp",
    ],
    deps = [
        ":task_executor_interface",
    ],
)

mongo_cc_library(
    name = "thread_pool_task_executor",
    srcs = [
        "thread_pool_task_executor.cpp",
    ],
    deps = [
        ":network_interface",
        ":task_executor_interface",
        "//src/mongo/db:service_context",
        "//src/mongo/util:future_util",
    ],
)

mongo_cc_library(
    name = "inline_executor",
    srcs = ["inline_executor.cpp"],
    deps = [
        ":task_executor_interface",
        "//src/mongo:base",
    ],
)

mongo_cc_library(
    name = "pinned_connection_task_executor_registry",
    srcs = [
        "pinned_connection_task_executor_registry.cpp",
    ],
    deps = [
        ":task_executor_interface",
    ],
)

mongo_cc_library(
    name = "pinned_connection_task_executor",
    srcs = [
        "pinned_connection_task_executor.cpp",
    ],
    deps = [
        "scoped_task_executor",
        "task_executor_interface",
        "//src/mongo/client:async_client",
    ],
)

mongo_cc_library(
    name = "pinned_connection_task_executor_factory",
    srcs = [
        "pinned_connection_task_executor_factory.cpp",
    ],
    deps = [
        ":network_interface",
        ":pinned_connection_task_executor",
        ":task_executor_interface",
        ":thread_pool_task_executor",
    ],
)

mongo_cc_library(
    name = "egress_connection_closer_manager",
    srcs = [
        "egress_connection_closer_manager.cpp",
    ],
    deps = [
        "//src/mongo/db:service_context",
    ],
)

idl_generator(
    name = "egress_networking_parameters_gen",
    src = "egress_networking_parameters.idl",
)

mongo_cc_library(
    name = "egress_networking_parameters",
    srcs = [
        "egress_networking_parameters.cpp",
        ":egress_networking_parameters_gen",
    ],
    deps = [
        "//src/mongo/db:server_base",
    ],
)

mongo_cc_library(
    name = "network_interface_tl",
    srcs = [
        "exhaust_response_reader_tl.cpp",
        "network_interface_tl.cpp",
    ],
    deps = [
        ":async_client_factory",
        ":network_interface",
        "//src/mongo/client:async_client",
        "//src/mongo/db:server_feature_flags",
        "//src/mongo/db/auth",
        "//src/mongo/db/commands/server_status:server_status_core",
        "//src/mongo/transport:transport_layer",
        "//src/mongo/transport:transport_layer_manager",
    ],
)

mongo_cc_library(
    name = "connection_pool_tl",
    srcs = [
        "connection_pool_tl.cpp",
    ],
    deps = [
        ":async_client_factory",
        ":connection_pool_executor",
        ":egress_networking_parameters",
        "//src/mongo/client:async_client",
        "//src/mongo/db/auth",
        "//src/mongo/db/commands/server_status:server_status_core",
        "//src/mongo/transport:transport_layer",
    ],
)

mongo_cc_library(
    name = "network_interface_factory",
    srcs = [
        "network_interface_factory.cpp",
    ],
    deps = [
        ":async_client_factory",
        ":connection_pool_executor",
        ":connection_pool_tl",
        ":egress_connection_closer_manager",
        ":network_interface",
        ":network_interface_tl",
    ] + select({
        "//bazel/config:build_grpc_enabled": ["//src/mongo/transport/grpc:grpc_async_client_factory"],
        "//conditions:default": [],
    }),
)

idl_generator(
    name = "task_executor_pool_parameters_gen",
    src = "task_executor_pool_parameters.idl",
)

mongo_cc_library(
    name = "task_executor_cursor",
    srcs = [
        "task_executor_cursor.cpp",
        "task_executor_cursor_options.cpp",
        ":task_executor_cursor_parameters_gen",
    ],
    deps = [
        ":pinned_connection_task_executor_factory",
        ":pinned_connection_task_executor_registry",
        ":task_executor_interface",
        "//src/mongo/db/query:command_request_response",
        "//src/mongo/db/query:plan_yield_policy",
    ],
)

mongo_cc_library(
    name = "task_executor_pool",
    srcs = [
        "task_executor_pool.cpp",
        "task_executor_pool_parameters_gen",
    ],
    deps = [
        ":task_executor_interface",
        "//src/mongo/db:server_base",
        "//src/mongo/util:processinfo",
    ],
)

mongo_cc_library(
    name = "async_request_executor",
    srcs = [
        "async_request_executor.cpp",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/db:service_context",
        "//src/mongo/util/concurrency:thread_pool",
    ],
)

idl_generator(
    name = "task_executor_cursor_parameters_gen",
    src = "task_executor_cursor_parameters.idl",
)

mongo_cc_library(
    name = "async_rpc_util",
    srcs = [
        "async_rpc_util.cpp",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/util:future_util",
    ],
)

mongo_cc_library(
    name = "async_rpc_error_info",
    srcs = [
        "async_rpc_error_info.cpp",
    ],
    deps = [
        "//src/mongo:base",
    ],
)

mongo_cc_library(
    name = "network_interface_thread_pool",
    srcs = [
        "network_interface_thread_pool.cpp",
    ],
    deps = [
        "//src/mongo:base",
    ],
)

mongo_cc_library(
    name = "connection_pool_stats",
    srcs = [
        "connection_pool_stats.cpp",
    ],
    deps = [
        "//src/mongo/db:server_base",
        "//src/mongo/db:server_feature_flags",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "async_client_factory",
    srcs = [
    ],
    deps = [
        ":connection_pool_stats",
        ":egress_connection_closer_manager",
        "//src/mongo/client:async_client",
        "//src/mongo/db:server_base",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "connection_pool_executor",
    srcs = [
        "connection_pool.cpp",
    ],
    deps = [
        ":async_client_factory",
        ":connection_pool_stats",
        ":egress_connection_closer_manager",
        ":remote_command",
        "//src/mongo/client:backoff_with_jitter",
        "//src/mongo/db:server_base",
        "//src/mongo/db:server_feature_flags",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "connection_pool_controllers",
    srcs = [
        "connection_pool_controllers.cpp",
    ],
    deps = [
        ":connection_pool_executor",
    ],
)

mongo_cc_library(
    name = "async_rpc",
    srcs = [
        "async_rpc.cpp",
    ],
    deps = [
        ":async_rpc_error_info",
        ":remote_command",
        ":task_executor_interface",
        "//src/mongo:base",
        "//src/mongo/db:commands",
        "//src/mongo/db:server_base",
        "//src/mongo/db/commands:kill_common",
        "//src/mongo/rpc:command_status",
        "//src/mongo/s/client:shard_interface",
    ],
)

mongo_cc_library(
    name = "mock_async_rpc",
    deps = [
        ":async_rpc",
    ],
)

mongo_cc_library(
    name = "network_interface_mock",
    srcs = [
        "mock_network_fixture.cpp",
        "network_interface_mock.cpp",
        "thread_pool_mock.cpp",
    ],
    deps = [
        ":network_interface",
        ":task_executor_interface",
        "//src/mongo:base",
        "//src/mongo/db:expression_context_builder",
        "//src/mongo/db:query_expressions",
        "//src/mongo/db:query_matcher",
        "//src/mongo/rpc:metadata",
        "//src/mongo/util:clock_source_mock",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "async_timer_mock",
    srcs = [
        "async_timer_mock.cpp",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/base:system_error",
    ],
)

mongo_cc_library(
    name = "network_test_env",
    srcs = [
        "network_test_env.cpp",
    ],
    deps = [
        ":network_interface_mock",
        ":task_executor_interface",
        "//src/mongo/db:commands",
        "//src/mongo/db/query:command_request_response",
        "//src/mongo/unittest",
    ],
)

mongo_cc_library(
    name = "task_executor_test_fixture",
    srcs = [
        "task_executor_test_common.cpp",
        "task_executor_test_fixture.cpp",
    ],
    deps = [
        ":network_interface_mock",
        ":task_executor_interface",
        "//src/mongo/unittest",
        "//src/mongo/util:clock_source_mock",
    ],
)

mongo_cc_library(
    name = "thread_pool_task_executor_test_fixture",
    srcs = [
        "thread_pool_task_executor_test_fixture.cpp",
    ],
    deps = [
        ":task_executor_test_fixture",
        ":thread_pool_task_executor",
    ],
)

mongo_cc_library(
    name = "network_interface_fixture",
    srcs = [
        "executor_integration_test_connection_stats.cpp",
        "network_interface_integration_fixture.cpp",
    ],
    deps = [
        "//src/mongo/executor:network_interface_factory",
        "//src/mongo/executor:network_interface_thread_pool",
        "//src/mongo/executor:thread_pool_task_executor",
        "//src/mongo/rpc:command_status",
        "//src/mongo/transport:grpc_connection_stats",
        "//src/mongo/unittest:integration_test_main",
        "//src/mongo/util:version_impl",
        "//src/mongo/util/concurrency:thread_pool",
    ],
)

mongo_cc_unit_test(
    name = "network_executor_test",
    srcs = [
        "async_multicaster_test.cpp",
        "async_rpc_test.cpp",
        "async_rpc_util_test.cpp",
        "connection_pool_test.cpp",
        "connection_pool_test_fixture.cpp",
        "mock_async_rpc_test.cpp",
        "mock_network_fixture_test.cpp",
        "network_interface_mock_test.cpp",
        "network_interface_mock_test_fixture.cpp",
        "pinned_connection_task_executor_test.cpp",
    ],
    tags = [
        "mongo_unittest_fifth_group",
    ],
    deps = [
        "async_rpc",
        "async_rpc_util",
        "connection_pool_executor",
        "egress_connection_closer_manager",
        "network_interface_mock",
        "network_interface_tl",
        "network_test_env",
        "pinned_connection_task_executor",
        "thread_pool_task_executor_test_fixture",
        "//src/mongo/client:remote_command_targeter",
        "//src/mongo/client:remote_command_targeter_mock",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/db/commands:standalone",
        "//src/mongo/db/repl:task_executor_mock",
        "//src/mongo/db/repl/hello:hello_command",
        "//src/mongo/db/shard_role:service_context_non_d",
        "//src/mongo/db/sharding_environment:unittest_noop_sharding_initialization_mongod",
        "//src/mongo/util:clock_source_mock",
    ],
)

mongo_cc_unit_test(
    name = "task_executor_test",
    srcs = [
        "cancelable_executor_test.cpp",
        "inline_executor_test.cpp",
        "scoped_task_executor_test.cpp",
        "split_timer_test.cpp",
        "task_executor_cursor_test.cpp",
        "thread_pool_task_executor_test.cpp",
    ],
    tags = [
        "mongo_unittest_fourth_group",
        "server-programmability",
    ],
    deps = [
        "inline_executor",
        "scoped_task_executor",
        "task_executor_cursor",
        "thread_pool_task_executor",
        "thread_pool_task_executor_test_fixture",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/db/shard_role:service_context_non_d",
    ],
)

mongo_cc_integration_test(
    name = "executor_integration_test",
    srcs = [
        "executor_integration_test_fixture.cpp",
        "exhaust_response_reader_integration_test.cpp",
        "network_interface_integration_test.cpp",
        "task_executor_cursor_integration_test.cpp",
        "thread_pool_task_executor_integration_test.cpp",
    ],
    deps = [
        "network_interface_fixture",
        "network_interface_tl",
        "task_executor_cursor",
        "//src/mongo/client:async_client",
        "//src/mongo/client:clientdriver_network",
        "//src/mongo/db:wire_version",
        "//src/mongo/db/commands:kill_operations_idl",
        "//src/mongo/db/query/client_cursor:cursor_response_idl",
        "//src/mongo/db/query/write_ops:write_ops_parsers",
        "//src/mongo/executor:network_interface_factory",
        "//src/mongo/executor:network_interface_thread_pool",
        "//src/mongo/executor:pinned_connection_task_executor",
        "//src/mongo/executor:thread_pool_task_executor",
        "//src/mongo/transport:transport_layer_egress_init",
        "//src/mongo/util:version_impl",
        "//src/mongo/util/concurrency:thread_pool",
        "//src/mongo/util/net:ssl_options_server",
    ],
)
